import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'package:flutter_provider_demo/providers/WeatherInfo.dart';
import 'SingleProviderDemo.dart';
import 'package:flutter_provider_demo/MultiProviderDemo.dart';
import 'providers/CounterBloc.dart';
import 'providers/ThemeDataBloc.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
            //类似于实例化一个WeatherInfo
            builder: (context) => WeatherInfo()),
        //这边使用dispose来控制销毁
        Provider(
          builder: (_) => CounterBloc(),
          dispose: (_, CounterBloc bloc) => bloc.dispose(),
        ),
        StreamProvider<ThemeData>.value(value:ThemeDataBloc().stream,initialData: ThemeData.light(),)
      ],
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Provider使用Demo',
        home: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('ProviderDemo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              onPressed: () {
                Navigator.of(context).push(new MaterialPageRoute(builder: (_) {
                  return SingleProviderDemo();
                }));
              },
              child: Text('单个的Provider使用'),
            ),
            RaisedButton(
              onPressed: () {
                Navigator.of(context).push(new MaterialPageRoute(builder: (_) {
                  return MultiProviderDemo();
                }));
              },
              child: Text('多个的Provider使用（结合Stream）'),
            ),
          ],
        ),
      ),
    );
  }
}
